/**
* JLModal v1.0
* Develop by LBN Studio
*/
;(function($) {
	
	$.fn.JLModal = function(options){
		$.fn.JLModal.initializing.init(this, options);
	};
	$.fn.JLModal.close = function(){
		$.fn.JLModal.initializing.close();
	};
	$.fn.JLModal.defaults = {
		duration: 300,
		opacity: 25,
		zIndex: 1000,
		titleHTML: "",
		overlayClass: 'JLModal-overlayClass',
		closeHTML: '<a class="JLModalClose" title="Fermer la fen&ecirc;tre (ESC)"></a>',
		onOpen: null,
		onShow: null,
		onClose: null,
		closeButton: true,
		escKey: true
	};
	$.fn.JLModal.initializing = {
		overlay: null,
		opt : null,
		d:{},
		container: null,
		overlayCss:{
			display:'none',
			height: '100%',
			width: '100%',
			position: 'fixed',
			left: 0,
			top: 0
		},
		close:function(){
			var s = this;
			
			if ($.isFunction(s.opt.onClose) && !s.occb) {
				s.occb = true;
				s.opt.onClose.apply(s, [s.d]);
			}else{
				$(s.overlay).fadeOut( s.opt.duration, function(){
					$(s.data).hide().appendTo('body');
					$(s.container).hide().remove();
					$(this).hide().remove();
				});
			}
		},
		init: function(obj, options){
			var s = this;
			s.data = obj;
			s.opt = $.extend( $.fn.JLModal.defaults, options );
			var opt = s.opt;
			
			obj.css("top", ($(window).height() - obj.height())/2  + 'px');
			obj.css("left", ($(window).width() - obj.width())/2  + 'px');
			s.overlay = $('<div></div>')
				.attr('id', 'overlayId')
				.addClass(opt.overlayClass)
				.css($.extend({}, s.overlayCss ))
				.appendTo('body')
			s.overlay.css('zIndex',opt.zIndex);
			s.overlay.css('opacity',opt.opacity/100);
			obj.css({
				zIndex:opt.zIndex+2, 
				position:'fixed', 
				margin:'0 auto',
				display:'none'
			});
			$('div.JLModal-title').html(opt.titleHTML);
			s.open();
			s.container = $('<div id="JLModalcontainer"></div>')
				.css({
					zIndex: opt.zIndex + 1,
					display:'none',
					position: 'fixed',
					height: obj.height(),
					width:obj.width(),
					top: ($(window).height() - obj.height())/2  + 'px',
					left: ($(window).width() - obj.width())/2  + 'px'
			}).prependTo('body');

			if(s.opt.closeButton){
				var _close = $(opt.closeHTML).addClass('JLModalClose').css({
					marginLeft: (obj.width()-33)+'px',
					marginTop: '15px'
				}).prependTo(s.container);
				obj.appendTo(s.container);

				_close.click(function(){
					s.close();
				});	
			}
			
			//data = null;
			s.occb = false;
			if ($.isFunction(opt.onShow)) {
				s.opt.onShow.apply(s, [s.d]);
			}
			
			s.defaultEvents();
		},
		open: function(){
			var s = this;
			$(s.overlay).fadeIn(s.opt.duration, function(){
				$('<input type="text"/>').css('display','none').appendTo(s.overlay).focus().select();
				$(s.container).show();
				$(s.data).show().focus().select();

				if($.isFunction(s.opt.onOpen)){
					s.opt.onOpen.apply(s, [s.d]);
				}
			});
		},
		watchTab: function (e){
			var s = this;

			if ($(e.target).parents('#JLModalcontainer').length > 0){
				// save the list of inputs
				s.inputs = $(':input:enabled:visible:first, :input:enabled:visible:last', s.data[0]);

				// if it's the first or last tabbable element, refocus
				if ((!e.shiftKey && e.target == s.inputs[s.inputs.length -1]) ||
						(e.shiftKey && e.target == s.inputs[0]) ||
						s.inputs.length == 0) {
					e.preventDefault();
					var pos = e.shiftKey ? 'last' : 'first';
					setTimeout(function () {s.overlay.focus(pos);}, 10);
				}
			}
			else{
				// might be necessary when custom onShow callback is used
				e.preventDefault();
				setTimeout(function () {s.overlay.focus();}, 10);
			}
		},
		defaultEvents: function(){
			var s = this;
			$(document).bind('keydown.JLModal', function(e){
				if (s.overlay.focus && e.keyCode == 9) { // TAB
					s.watchTab(e);
				}
				else if (s.overlay.focus && e.keyCode == 27) { // TAB
					if(s.opt.escKey) s.close();
				}
			});
		}
	};
})(jQuery);